<script setup lang="ts">
import { GetPreOrderInfoAPI, GetPreOrderInfoNowAPI, POSTOrderAPI } from '@/apis/Order'
import { useAddressStore } from '@/stores/address'
import type { CustomPaymentInstance } from '@/types/global-components'
import type { GetPreOrderInfoRes, POSTOrderRes } from '@/types/Order'
import { onLoad } from '@dcloudio/uni-app'
import { computed, ref } from 'vue'
import PaymentPopup from '@/components/payment-popup/payment-popup.vue'

// 获取屏幕边界到安全区域距离
const { safeAreaInsets } = uni.getSystemInfoSync()
// 订单备注
const buyerMessage = ref('')
// 配送时间
const deliveryList = ref([
  { type: 1, text: '时间不限 (周一至周日)' },
  { type: 2, text: '工作日送 (周一至周五)' },
  { type: 3, text: '周末配送 (周六至周日)' },
])
// 当前配送时间下标
const activeIndex = ref(0)
// 当前配送时间
const activeDelivery = computed(() => deliveryList.value[activeIndex.value])
// 修改配送时间
const onChangeDelivery: UniHelper.SelectorPickerOnChange = (ev) => {
  activeIndex.value = ev.detail.value
}

// 这是什么Id=> 商品Id
const query = defineProps<{
  skuId: string
  count: string
}>()

// 获取预支付订单信息
const PreOrderInfo = ref<GetPreOrderInfoRes>()
const GetPreOrderInfo = async () => {
  if (query.count && query.skuId) {
    const res = await GetPreOrderInfoNowAPI({
      skuId: query.skuId,
      count: query.count,
    })
    PreOrderInfo.value = res.result
  } else {
    const res = await GetPreOrderInfoAPI()
    // console.log(res)
    PreOrderInfo.value = res.result
  }
}
onLoad(() => {
  GetPreOrderInfo()
})

// 获取收货地址
const selectedAddress = computed(() => {
  return (
    useAddressStore().selectedAddress || PreOrderInfo.value?.userAddresses.find((v) => v.isDefault)
  )
})

// 支付弹窗实例
const paymentPopupRef = ref()
// 订单ID
const orderId = ref('')

// 立即支付
const handlePay = async () => {
  // 判断是否有地址
  if (!selectedAddress.value?.id) {
    return uni.showToast({
      title: '请选择收货地址',
      icon: 'error',
    })
  }

  // 创建订单
  if (PreOrderInfo.value) {
    const res = await POSTOrderAPI({
      addressId: selectedAddress.value?.id,
      buyerMessage: buyerMessage.value,
      deliveryTimeType: activeDelivery.value.type,
      goods: PreOrderInfo.value!.goods.map((item) => ({
        count: item.count,
        skuId: item.skuId,
      })),
      payChannel: 1,
      payType: 1,
    })
    // // 保存订单Id
    orderId.value = res.result.id

    // // 打开支付弹窗
    // paymentPopupRef.value?.open()
    // 关闭当前页,跳转到订单详情页,并传递订单Id
    uni.redirectTo({ url: `/subPackages_Order/OrderDetail/OrderDetail?id=${res.result.id}` })
  }
}

// 点击关闭按钮或遮罩层
const handlleClose = async () => {
  // 二次确认
  const res = await uni.showModal({
    title: '关闭支付',
    content: '取消支付将无法获得商品,是否确认关闭',
    cancelText: '仍要关闭',
    cancelColor: '#848484',
    confirmText: '确认关闭',
    confirmColor: '#16C2A3',
  })
  if (res.cancel) {
    console.log(res.cancel)

    // 如果用户选择关闭,则关闭支付弹窗
    paymentPopupRef.value?.close()
  }
}

// 确认支付
const handleConfirm = async (index: number) => {
  if (index === 1) {
    uni.showToast({ title: '微信支付暂不支持', icon: 'none' })
  } else if (index === 2) {
    // 基于当前网站,拼接支付回调地址
    const href = location.href
    // 获取当前网站的协议
    const webUrl = href.split('#')[0]
    // 拼接回调地址
    const payCallback = `${webUrl}#/subPackages_Order/pay_Result/pay_Result`
  }
}
</script>

<template>
  <scroll-view scroll-y class="viewport">
    <!-- 收货地址 -->
    <navigator
      v-if="selectedAddress"
      class="shipment"
      hover-class="none"
      url="/subPackages_settings/address/address"
    >
      <view class="user"> {{ selectedAddress?.receiver }} {{ selectedAddress?.contact }} </view>
      <view class="address">
        {{ selectedAddress?.fullLocation }} {{ selectedAddress?.address }}
      </view>
      <text class="icon icon-right"></text>
    </navigator>
    <navigator
      v-else
      class="shipment"
      hover-class="none"
      url="/subPackages_settiings/address/address"
    >
      <view class="address"> 请选择收货地址 </view>
      <text class="icon icon-right"></text>
    </navigator>

    <!-- 商品信息 -->
    <view class="goods">
      <navigator
        v-for="item in PreOrderInfo?.goods"
        :key="item.id"
        :url="`/pages/goods/goods?id=${item.id}`"
        class="item"
        hover-class="none"
      >
        <image class="picture" :src="item.picture" />
        <view class="meta">
          <view class="name ellipsis"> {{ item.name }} </view>
          <view class="attrs"> {{ item.attrsText }} </view>
          <view class="prices">
            <view class="pay-price symbol"> {{ item.payPrice }} </view>
            <view class="price symbol"> {{ item.price }} </view>
          </view>
          <view class="count">x{{ item.count }}</view>
        </view>
      </navigator>
    </view>

    <!-- 配送及支付方式 -->
    <view class="related">
      <view class="item">
        <text class="text">配送时间</text>
        <picker :range="deliveryList" range-key="text" @change="onChangeDelivery">
          <view class="icon-fonts picker">{{ activeDelivery.text }}</view>
        </picker>
      </view>
      <view class="item">
        <text class="text">订单备注</text>
        <input
          class="input"
          :cursor-spacing="30"
          placeholder="选题，建议留言前先与商家沟通确认"
          v-model="buyerMessage"
        />
      </view>
    </view>

    <!-- 支付金额 -->
    <view class="settlement">
      <view class="item">
        <text class="text">商品总价: </text>
        <text class="number symbol">{{ PreOrderInfo?.summary.totalPrice.toFixed(2) }}</text>
      </view>
      <view class="item">
        <text class="text">运费: </text>
        <text class="number symbol">{{ PreOrderInfo?.summary.postFee.toFixed(2) }}</text>
      </view>
    </view>
  </scroll-view>

  <!-- 吸底工具栏 -->
  <view class="toolbar" :style="{ paddingBottom: safeAreaInsets?.bottom + 'px' }">
    <view class="total-pay symbol">
      <text class="number">{{ PreOrderInfo?.summary.totalPayPrice.toFixed(2) }}</text>
    </view>
    <view class="button" :class="{ disabled: !selectedAddress?.id }" @click="handlePay">
      提交订单
    </view>
  </view>

  <!-- 支付弹窗 -->
  <payment-popup
    ref="paymentPopupRef"
    :total-amount="PreOrderInfo?.summary.totalPayPrice || 0"
    @close="handlleClose"
    @confirm="handleConfirm"
  />
</template>

<style lang="scss">
page {
  display: flex;
  flex-direction: column;
  height: 100%;
  overflow: hidden;
  background-color: #f4f4f4;
}

.symbol::before {
  content: '¥';
  font-size: 80%;
  margin-right: 5rpx;
}

.shipment {
  margin: 20rpx;
  padding: 30rpx 30rpx 30rpx 84rpx;
  font-size: 26rpx;
  border-radius: 10rpx;
  background: url(https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/images/locate.png)
    20rpx center / 50rpx no-repeat #fff;
  position: relative;

  .icon {
    font-size: 36rpx;
    color: #333;
    transform: translateY(-50%);
    position: absolute;
    top: 50%;
    right: 20rpx;
  }

  .user {
    color: #333;
    margin-bottom: 5rpx;
  }

  .address {
    color: #666;
  }
}

.goods {
  margin: 20rpx;
  padding: 0 20rpx;
  border-radius: 10rpx;
  background-color: #fff;

  .item {
    display: flex;
    padding: 30rpx 0;
    border-top: 1rpx solid #eee;

    &:first-child {
      border-top: none;
    }

    .picture {
      width: 170rpx;
      height: 170rpx;
      border-radius: 10rpx;
      margin-right: 20rpx;
    }

    .meta {
      flex: 1;
      display: flex;
      flex-direction: column;
      justify-content: center;
      position: relative;
    }

    .name {
      height: 80rpx;
      font-size: 26rpx;
      color: #444;
    }

    .attrs {
      line-height: 1.8;
      padding: 0 15rpx;
      margin-top: 6rpx;
      font-size: 24rpx;
      align-self: flex-start;
      border-radius: 4rpx;
      color: #888;
      background-color: #f7f7f8;
    }

    .prices {
      display: flex;
      align-items: baseline;
      margin-top: 6rpx;
      font-size: 28rpx;

      .pay-price {
        margin-right: 10rpx;
        color: #cf4444;
      }

      .price {
        font-size: 24rpx;
        color: #999;
        text-decoration: line-through;
      }
    }

    .count {
      position: absolute;
      bottom: 0;
      right: 0;
      font-size: 26rpx;
      color: #444;
    }
  }
}

.related {
  margin: 20rpx;
  padding: 0 20rpx;
  border-radius: 10rpx;
  background-color: #fff;

  .item {
    display: flex;
    justify-content: space-between;
    align-items: center;
    min-height: 80rpx;
    font-size: 26rpx;
    color: #333;
  }

  .input {
    flex: 1;
    text-align: right;
    margin: 20rpx 0;
    padding-right: 20rpx;
    font-size: 26rpx;
    color: #999;
  }

  .item .text {
    width: 125rpx;
  }

  .picker {
    color: #666;
  }

  .picker::after {
    content: '\e6c2';
  }
}

/* 结算清单 */
.settlement {
  margin: 20rpx;
  padding: 0 20rpx;
  border-radius: 10rpx;
  background-color: #fff;

  .item {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 80rpx;
    font-size: 26rpx;
    color: #333;
  }

  .danger {
    color: #cf4444;
  }
}

/* 吸底工具栏 */
.toolbar {
  position: fixed;
  left: 0;
  right: 0;
  bottom: calc(var(--window-bottom));
  z-index: 1;

  background-color: #fff;
  height: 100rpx;
  padding: 0 20rpx;
  border-top: 1rpx solid #eaeaea;
  display: flex;
  justify-content: space-between;
  align-items: center;
  box-sizing: content-box;

  .total-pay {
    font-size: 40rpx;
    color: #cf4444;

    .decimal {
      font-size: 75%;
    }
  }

  .button {
    width: 220rpx;
    text-align: center;
    line-height: 72rpx;
    font-size: 26rpx;
    color: #fff;
    border-radius: 72rpx;
    background-color: #27ba9b;
  }

  .disabled {
    opacity: 0.6;
  }
}
</style>
